// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   FileUtil.java

package com.boci.common.util;

import java.io.*;

public class FileUtil
{

    public FileUtil()
    {
    }

    public static BufferedReader getReader(InputStream is, String encoding)
        throws IOException, UnsupportedEncodingException
    {
        BufferedReader reader = null;
        PushbackInputStream pis = new PushbackInputStream(is, 1024);
        String bomEncoding = getBOMEncoding(pis);
        if(bomEncoding == null)
            reader = new BufferedReader(new InputStreamReader(pis, encoding));
        else
            reader = new BufferedReader(new InputStreamReader(pis, bomEncoding));
        return reader;
    }

    protected static String getBOMEncoding(PushbackInputStream is)
        throws IOException
    {
        String encoding = null;
        int bytes[] = new int[3];
        bytes[0] = is.read();
        bytes[1] = is.read();
        bytes[2] = is.read();
        if(bytes[0] == 254 && bytes[1] == 255)
        {
            encoding = "UTF-16BE";
            is.unread(bytes[2]);
        } else
        if(bytes[0] == 255 && bytes[1] == 254)
        {
            encoding = "UTF-16LE";
            is.unread(bytes[2]);
        } else
        if(bytes[0] == 239 && bytes[1] == 187 && bytes[2] == 191)
        {
            encoding = "UTF-8";
        } else
        {
            for(int i = bytes.length - 1; i >= 0; i--)
                is.unread(bytes[i]);

        }
        return encoding;
    }

    private static final String ENCODE_UTF8 = "UTF-8";
    private static final String ENCODE_UTF_16BE = "UTF-16BE";
    private static final String ENCODE_UTF_16LE = "UTF-16LE";
}
